package com.sz.lk.wingsystem.madapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sz.lk.wingsystem.R;

import java.util.List;

/**
 * author : liuke
 * e-mail : 1012459343@qq.com
 * date   : 2020/12/6 22:43
 * desc   :L
 * version: 1.0
 */
public class MyListAdapter extends BaseQuickAdapter<BluetoothDevice, BaseViewHolder> {
    private Context context;

    public MyListAdapter(@Nullable List<BluetoothDevice> data, Context context) {
        super(R.layout.layout_item_bt_device, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, BluetoothDevice item) {
        helper.addOnClickListener(R.id.device_pair);
        int code = item.getBondState();
        String name = item.getName();
        String mac = item.getAddress();
        String state;
        if (name == null || name.length() == 0) {
            name = "未命名设备";
        }
        if (code == BluetoothDevice.BOND_BONDED) {
            state = "ready";
            helper.setTextColor(R.id.device_state, context.getResources().getColor(R.color.green));
        } else {
            state = "new";
            helper.setTextColor(R.id.device_state, context.getResources().getColor(R.color.red));
        }
        if (mac == null || mac.length() == 0) {
            mac = "未知 mac 地址";
        }
        helper.setText(R.id.device_name, name);
        helper.setText(R.id.device_mac, mac);
        helper.setText(R.id.device_state, state);
        if (item.getBondState() == BluetoothDevice.BOND_BONDED) {
            helper.setText(R.id.device_pair, R.string.repaired);
        }
    }
}